package com.nondo.dean.platform.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.platform.domain.Customer;
import com.nondo.dean.platform.domain.CustomerExample;
import com.nondo.dean.platform.mapper.CustomerMapper;
import com.nondo.dean.platform.model.CustomerBean;

/**
 * 客户管理业务类
 * 
 * @author zhaoljin
 * @date 2018年5月7日
 */
@Component
public class CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 插入客户
     * @param customer
     * @return
     */
    @Transactional
    public int addCustomer(Customer customer) {
        return customerMapper.insertSelective(customer);
    }

    /**
     * 修改客户
     * @param customer
     * @return
     */
    @Transactional
    public int updateCustomer(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    /**
     * 按照主键ID查询客户
     * @param id
     * @return
     */
    public Customer selectCustomer(Integer id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    /**
     * 客户分页查询列表
     * @param offset
     * @param limit
     * @param customer
     * @return
     */
    public List<Customer> getCustomerList(int offset, int limit, Customer customer) {
        CustomerExample example = new CustomerExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        CustomerExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, customer);
        return customerMapper.selectByExampleWithRowbounds(example, rowBounds);
    }
    
    /**
     * 按照活动ID和手机号统计
     * @param customer
     * @return
     */
    public Long countByActivityIdAndPhone(Customer customer) {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        if (customer.getActivityId() != null)
            criteria.andActivityIdEqualTo(customer.getActivityId());
        if (StringUtils.isNoneBlank(customer.getPhone()))
            criteria.andPhoneEqualTo(customer.getPhone());
        criteria.andDelFlagEqualTo(Customer.DEL_FLAG_NORMAL);
        return customerMapper.countByExample(example);
    }
    
    /**
     * 客户总数统计
     * @param customer
     * @return
     */
    public Long count(Customer customer) {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, customer);
        return customerMapper.countByExample(example);
    }

    /**
     * 组装查询条件
     * @param criteria
     * @param customer
     */
    void setCriteria(CustomerExample.Criteria criteria, Customer customer) {
        if (customer != null) {
            if (customer.getId() != null)
                criteria.andIdEqualTo(customer.getId());
            if (customer.getUserId() != null)
                criteria.andUserIdEqualTo(customer.getUserId());
            if (customer.getActivityId() != null)
                criteria.andActivityIdEqualTo(customer.getActivityId());
            if (StringUtils.isNoneBlank(customer.getPhone()))
                criteria.andPhoneEqualTo(customer.getPhone());
            if (StringUtils.isNoneBlank(customer.getName()))
                criteria.andNameLike("%" + customer.getName() + "%");
        }
        criteria.andDelFlagEqualTo(Customer.DEL_FLAG_NORMAL);
    }

   /**
    * 按照客户名称查询客户
    * @param name
    * @return
    */
    public Customer selectCustomerByName(String name) {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        criteria.andDelFlagEqualTo(Customer.DEL_FLAG_NORMAL);
        List<Customer> customerList = customerMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(customerList)?customerList.get(0):null;
    }

    /**
     * 根据openid查询用户
     * 
     * @param openid
     * @return
     */
    public Customer selectCustomerByOpenid(String openid) {
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        criteria.andOpenidEqualTo(openid);
        criteria.andDelFlagEqualTo(Customer.DEL_FLAG_NORMAL);
        List<Customer> customerList = customerMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(customerList) ? customerList.get(0) : null;
    }

    
    /**
     * 摇一摇次数排行榜列表
     * @return List<{@link CustomerBean}
     */
    public List<Customer> getShakeRankList(int offset, int limit, Customer customer) {
        CustomerExample example = new CustomerExample();
        example.setOrderByClause("shake_num DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        CustomerExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, customer);
        return customerMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 清空所有摇一摇次数
     */
    @Transactional
    public void cleanShakeNum() {
        Customer customer = new Customer();
        customer.setShakeNum(0);
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, customer);
        customerMapper.updateByExampleSelective(customer, example);
    }

    /**
     * 按用户名和联系方式统计
     * @param name
     * @return
     */
    public Long countSearch(Customer customer){
        CustomerExample example = new CustomerExample();
        CustomerExample.Criteria criteria1 = example.createCriteria();
        criteria1.andUserIdEqualTo(customer.getUserId());
        criteria1.andNameLike("%"+customer.getName()+"%");
        CustomerExample.Criteria criteria2 = example.createCriteria();
        criteria2.andPhoneLike("%"+customer.getName()+"%");
        criteria2.andUserIdEqualTo(customer.getUserId());
        example.or(criteria2);
        return customerMapper.countByExample(example);
    }

    /**
     * 按用户名和联系方式查询
     * @param offset
     * @param limit
     * @param name
     * @return
     */
    public List<Customer> getSearchList(int offset,int limit,Customer customer){
        RowBounds rowBounds = new RowBounds(offset, limit);
        CustomerExample example = new CustomerExample();
        example.setOrderByClause("create_date DESC");
        CustomerExample.Criteria criteria1 = example.createCriteria();
        criteria1.andUserIdEqualTo(customer.getUserId());
        criteria1.andNameLike("%"+customer.getName()+"%");
        CustomerExample.Criteria criteria2 = example.createCriteria();
        criteria2.andPhoneLike("%"+customer.getName()+"%");
        criteria2.andUserIdEqualTo(customer.getUserId());
        example.or(criteria2);
        return  customerMapper.selectByExampleWithRowbounds(example,rowBounds);
    }

    /**
     * 按id查询
     * @param id
     * @return
     */
    public Customer selectId(Integer id){
        return customerMapper.selectByPrimaryKey(id);
    }

}
